package com.qeegoo.b2bautozityp.moudle.home.view;

import android.os.Bundle;
import android.support.design.widget.NavigationView.OnNavigationItemSelectedListener;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.MenuItem;
import android.widget.TextView;

import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.api.HttpConsts;
import com.qeegoo.b2bautozityp.constant.Constants;
import com.qeegoo.b2bautozityp.databinding.ActivityMainBinding;
import com.qeegoo.b2bautozityp.injector.component.DaggerMainComponent;
import com.qeegoo.b2bautozityp.moudle.base.AppBar;
import com.qeegoo.b2bautozityp.moudle.base.BaseActivity;
import com.qeegoo.b2bautozityp.moudle.home.viewmodel.MainViewModel;
import com.qeegoo.b2bautozityp.moudle.user.view.AccountSecurityActivity;
import com.qeegoo.b2bautozityp.moudle.user.view.HelpActivity;
import com.qeegoo.b2bautozityp.moudle.webview.WebViewActivity;
import com.qeegoo.b2bautozityp.moudle.webview.WebViewActivity.Extra;

import javax.inject.Inject;

import base.lib.util.NavigateUtils;
import base.lib.util.PreferencesUtils;

/**
 * 王少岩 在 2017/9/8 创建了它
 */

public class MainActivity extends BaseActivity<ActivityMainBinding> {
    @Inject
    AppBar mAppBar;
    @Inject
    MainViewModel mViewModel;
    private TextView name;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_main;
    }

    @Override
    protected void initInjector() {
        DaggerMainComponent.builder().appComponent(getAppComponent()).build().inject(this);
    }

    @Override
    protected void initViews() {
        mBinding.drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        mAppBar.setleftCommon(() -> mBinding.drawerLayout.openDrawer(Gravity.START));
        mBinding.setAppbar(mAppBar);
        mBinding.setViewModel(mViewModel);
        name = (TextView)mBinding.navView.getHeaderView(0).findViewById(R.id.name);
        mBinding.navView.setNavigationItemSelectedListener(new OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected( MenuItem item) {

                switch (item.getTitle().toString()){
                    case "账户与安全":{
                        NavigateUtils.startActivity(AccountSecurityActivity.class);
                        break;
                    }
                    case "帮助中心":{
                        NavigateUtils.startActivity(HelpActivity.class);
                        break;
                    }
                    case "法律条款":{
                        String url = HttpConsts.getServer()+"api/manage/supplier/helpCenter/gotoLawHtml";
                        Bundle bundle = new Bundle();
                        bundle.putString(Extra.kIn_title,"法律条款");
                        bundle.putString(Extra.kIn_url,url);
                        NavigateUtils.startActivity(WebViewActivity.class,bundle);
                        break;
                    }
                    case "联系我们":{
                        String url = HttpConsts.getServer()+"api/manage/supplier/user/contactWe";
                        Bundle bundle = new Bundle();
                        bundle.putString(Extra.kIn_title,"联系我们");
                        bundle.putString(Extra.kIn_url,url);
                        NavigateUtils.startActivity(WebViewActivity.class,bundle);
                        break;
                    }
                    default:
                        break;
                }
                mBinding.drawerLayout.closeDrawers();
                return true;
            }
        });
//        name.setText(PreferencesUtils.getString(Constants.sUser_username, ""));
        name.setText(PreferencesUtils.getString(Constants.sUser_partyName, ""));
    }
    public void showDrawerLayout(){
        mBinding.drawerLayout.openDrawer(Gravity.START);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mViewModel.loadHttp();
    }
}
